/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitee.minimalismstyle.fresh.common.data.jdbc.service.impl

import com.gitee.minimalismstyle.fresh.common.core.domain.R
import com.gitee.minimalismstyle.fresh.common.data.commons.service.BaseService
import com.gitee.minimalismstyle.fresh.common.data.jdbc.dao.BaseDao
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.transaction.annotation.Transactional

/**
 * jdbc 基础service
 * @author maoxiaodong
 */
abstract class BaseServiceImpl<DAO : BaseDao<T, ID>, T, ID> : BaseService<T, ID> {

    @Autowired
    protected lateinit var dao: DAO

    override fun findById(id: ID): R<T> = R.success(dao.findById(id).get())

    override fun findByIds(ids: List<ID>): R<List<T>> = R.success(dao.findAllById(ids.asIterable()).toList())

    override fun findAll(): R<List<T>> = R.success(dao.findAll().toList())

    @Transactional
    override fun save(t: T): R<T> {
        dao.save(t)
        return R.success(t)
    }

    override fun saveAll(ts: List<T>): R<List<T>> {
        dao.saveAll(ts)
        return R.success(ts)
    }

    @Transactional
    override fun deleteById(id: ID): R<Int> {
        dao.deleteById(id)
        return R.success()
    }

    override fun deleteByIds(ids: List<ID>): R<Int> {
        dao.deleteAllById(ids)
        return R.success(1)
    }


    override fun isUnique(t: T): R<Boolean> {
        return R.success(false)
    }


}
